package com.lottery.controller;

import com.lottery.dto.AppPrizesFromWinningHistoryOutputDTO;
import com.lottery.pojo.TableRegions;
import com.lottery.pojo.TableWinningHistories;
import com.lottery.service.RegionService;
import com.lottery.service.WinningHistoriesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class WinningHistoriesController {

    @Autowired
    private WinningHistoriesService winningHistoriesService;

    /**
     * []localhost:8001/getWinningHistoryByDateRange?machine_number=1&startTime=2022-02-28&endTime=2022-02-29
     */
    @PostMapping("getWinningHistoryByDateRange")
    public List<TableWinningHistories> getWinningHistoryByDateRange(String machine_number, String startTime, String endTime) {
        return winningHistoriesService.getOrdersByDateRange(machine_number, startTime, endTime);
    }

    //-------------app
    @Autowired
    RegionService regionService;

    @PostMapping("appGetPrizesFromWinningHistory")
    private AppPrizesFromWinningHistoryOutputDTO appGetPrizesFromWinningHistory(String machine_number, String startTime, String endTime) {
        String general = "general";
        String grand = "grand";
        String consolation = "consolation";

        AppPrizesFromWinningHistoryOutputDTO outputDTO = new AppPrizesFromWinningHistoryOutputDTO();
        AppPrizesFromWinningHistoryOutputDTO.WinningHistories winningHistories = new AppPrizesFromWinningHistoryOutputDTO.WinningHistories();

        List<TableRegions> regions = regionService.getRegions();
        for (TableRegions region : regions) {
            switch (region.getRegion_key()) {
                case "K":
                    com.lottery.dto.AppPrizesFromWinningHistoryOutputDTO.WinningHistories.K k = new AppPrizesFromWinningHistoryOutputDTO.WinningHistories.K();
                    k.setRegionName(region.getRegion_name());
                    k.setRank1Digit4(getPrizesFromWinningHistory(machine_number, region.getId(), general, 1, 4, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    k.setRank2Digit4(getPrizesFromWinningHistory(machine_number, region.getId(), general, 2, 4, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    k.setRank3Digit4(getPrizesFromWinningHistory(machine_number, region.getId(), general, 3, 4, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    k.setRank1Digit3(getPrizesFromWinningHistory(machine_number, region.getId(), general, 1, 3, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    k.setRank2Digit3(getPrizesFromWinningHistory(machine_number, region.getId(), general, 2, 3, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    k.setRank3Digit3(getPrizesFromWinningHistory(machine_number, region.getId(), general, 3, 3, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    k.setRank1Digit2(getPrizesFromWinningHistory(machine_number, region.getId(), general, 1, 2, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    k.setRank2Digit2(getPrizesFromWinningHistory(machine_number, region.getId(), general, 2, 2, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    k.setRank3Digit2(getPrizesFromWinningHistory(machine_number, region.getId(), general, 3, 2, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    k.setGrandDigit4(getPrizesFromWinningHistory(machine_number, region.getId(), grand, 1, 4, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    k.setGrandDigit3(getPrizesFromWinningHistory(machine_number, region.getId(), grand, 1, 3, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    k.setGrandDigit2(getPrizesFromWinningHistory(machine_number, region.getId(), grand, 1, 2, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    k.setConsolePrize(getPrizesFromWinningHistory(machine_number, region.getId(), consolation, 1, 4, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    k.setTotalPrize(getPrizesFromWinningHistory(machine_number, region.getId(), null, -1, -1, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    winningHistories.setK(k);
                    break;
                case "F":
                    com.lottery.dto.AppPrizesFromWinningHistoryOutputDTO.WinningHistories.F f = new AppPrizesFromWinningHistoryOutputDTO.WinningHistories.F();
                    f.setRegionName(region.getRegion_name());
                    f.setRank1Digit4(getPrizesFromWinningHistory(machine_number, region.getId(), general, 1, 4, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    f.setRank2Digit4(getPrizesFromWinningHistory(machine_number, region.getId(), general, 2, 4, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    f.setRank3Digit4(getPrizesFromWinningHistory(machine_number, region.getId(), general, 3, 4, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    f.setRank1Digit3(getPrizesFromWinningHistory(machine_number, region.getId(), general, 1, 3, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    f.setRank2Digit3(getPrizesFromWinningHistory(machine_number, region.getId(), general, 2, 3, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    f.setRank3Digit3(getPrizesFromWinningHistory(machine_number, region.getId(), general, 3, 3, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    f.setRank1Digit2(getPrizesFromWinningHistory(machine_number, region.getId(), general, 1, 2, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    f.setRank2Digit2(getPrizesFromWinningHistory(machine_number, region.getId(), general, 2, 2, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    f.setRank3Digit2(getPrizesFromWinningHistory(machine_number, region.getId(), general, 3, 2, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    f.setGrandDigit4(getPrizesFromWinningHistory(machine_number, region.getId(), grand, 1, 4, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    f.setGrandDigit3(getPrizesFromWinningHistory(machine_number, region.getId(), grand, 1, 3, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    f.setGrandDigit2(getPrizesFromWinningHistory(machine_number, region.getId(), grand, 1, 2, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    f.setConsolePrize(getPrizesFromWinningHistory(machine_number, region.getId(), consolation, 1, 4, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    f.setTotalPrize(getPrizesFromWinningHistory(machine_number, region.getId(), null, -1, -1, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    winningHistories.setF(f);
                    break;
                case "D":
                    com.lottery.dto.AppPrizesFromWinningHistoryOutputDTO.WinningHistories.D d = new AppPrizesFromWinningHistoryOutputDTO.WinningHistories.D();
                    d.setRegionName(region.getRegion_name());
                    d.setRank1Digit4(getPrizesFromWinningHistory(machine_number, region.getId(), general, 1, 4, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    d.setRank2Digit4(getPrizesFromWinningHistory(machine_number, region.getId(), general, 2, 4, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    d.setRank3Digit4(getPrizesFromWinningHistory(machine_number, region.getId(), general, 3, 4, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    d.setRank1Digit3(getPrizesFromWinningHistory(machine_number, region.getId(), general, 1, 3, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    d.setRank2Digit3(getPrizesFromWinningHistory(machine_number, region.getId(), general, 2, 3, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    d.setRank3Digit3(getPrizesFromWinningHistory(machine_number, region.getId(), general, 3, 3, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    d.setRank1Digit2(getPrizesFromWinningHistory(machine_number, region.getId(), general, 1, 2, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    d.setRank2Digit2(getPrizesFromWinningHistory(machine_number, region.getId(), general, 2, 2, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    d.setRank3Digit2(getPrizesFromWinningHistory(machine_number, region.getId(), general, 3, 2, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    d.setGrandDigit4(getPrizesFromWinningHistory(machine_number, region.getId(), grand, 1, 4, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    d.setGrandDigit3(getPrizesFromWinningHistory(machine_number, region.getId(), grand, 1, 3, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    d.setGrandDigit2(getPrizesFromWinningHistory(machine_number, region.getId(), grand, 1, 2, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    d.setConsolePrize(getPrizesFromWinningHistory(machine_number, region.getId(), consolation, 1, 4, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    d.setTotalPrize(getPrizesFromWinningHistory(machine_number, region.getId(), null, -1, -1, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    winningHistories.setD(d);
                    break;
                case "S":
                    com.lottery.dto.AppPrizesFromWinningHistoryOutputDTO.WinningHistories.S s = new AppPrizesFromWinningHistoryOutputDTO.WinningHistories.S();
                    s.setRegionName(region.getRegion_name());
                    s.setRank1Digit4(getPrizesFromWinningHistory(machine_number, region.getId(), general, 1, 4, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    s.setRank2Digit4(getPrizesFromWinningHistory(machine_number, region.getId(), general, 2, 4, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    s.setRank3Digit4(getPrizesFromWinningHistory(machine_number, region.getId(), general, 3, 4, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    s.setRank1Digit3(getPrizesFromWinningHistory(machine_number, region.getId(), general, 1, 3, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    s.setRank2Digit3(getPrizesFromWinningHistory(machine_number, region.getId(), general, 2, 3, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    s.setRank3Digit3(getPrizesFromWinningHistory(machine_number, region.getId(), general, 3, 3, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    s.setRank1Digit2(getPrizesFromWinningHistory(machine_number, region.getId(), general, 1, 2, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    s.setRank2Digit2(getPrizesFromWinningHistory(machine_number, region.getId(), general, 2, 2, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    s.setRank3Digit2(getPrizesFromWinningHistory(machine_number, region.getId(), general, 3, 2, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    s.setGrandDigit4(getPrizesFromWinningHistory(machine_number, region.getId(), grand, 1, 4, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    s.setGrandDigit3(getPrizesFromWinningHistory(machine_number, region.getId(), grand, 1, 3, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    s.setGrandDigit2(getPrizesFromWinningHistory(machine_number, region.getId(), grand, 1, 2, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    s.setConsolePrize(getPrizesFromWinningHistory(machine_number, region.getId(), consolation, 1, 4, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    s.setTotalPrize(getPrizesFromWinningHistory(machine_number, region.getId(), null, -1, -1, startTime, endTime).stream().mapToDouble(TableWinningHistories::getPrizes).summaryStatistics().getSum());
                    winningHistories.setS(s);
                    break;
            }
        }
        outputDTO.setWinningHistories(winningHistories);
        return outputDTO;
    }

    private List<TableWinningHistories> getPrizesFromWinningHistory(String machine_number, int region_id, String prizes_type, int rank, int digit, String startTime, String endTime) {
        return winningHistoriesService.getPrizesFromWinningHistory(machine_number, region_id, prizes_type, rank, digit, startTime, endTime);
    }
}
